package com.xyauto.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.xyauto.helper.OnDragVHListener;
import com.xyauto.helper.OnItemMoveListener;
import com.xyauto.qiehuan.Module;
import com.xyauto.xfq.R;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;


/**
 * 仅拖拽排序
 * Created by YoKeyword on 16/1/4.
 */
public class DragAdapter extends RecyclerView.Adapter<DragAdapter.DragViewHolder> implements OnItemMoveListener {
    private List<Module> mItems;
    private LayoutInflater mInflater;

    public DragAdapter(Context context, List<Module> items) {
        mInflater = LayoutInflater.from(context);
        this.mItems = items;
    }

    @Override
    public DragViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        final DragViewHolder myHolder = new DragViewHolder(mInflater.inflate(R.layout.item_drag, parent, false));
        myHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mChannelItemClickListener!=null){
                    mChannelItemClickListener.onItemClick(v, myHolder.getAdapterPosition());
                }
            }
        });
        myHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mChannelItemLongClickListener!=null){
                    return mChannelItemLongClickListener.onItemLongClick(v, myHolder.getAdapterPosition());
                }
                return false;
            }
        });
        return myHolder;
    }

    @Override
    public void onBindViewHolder(DragViewHolder holder, int position) {
        holder.tv.setText(mItems.get(position).getmName_id());
        holder.img.setImageResource(mItems.get(position).getIcon());
        if (mItems.get(position).isSelect()){
            holder.itemView.setSelected(true);
        }else {
            holder.itemView.setSelected(false);
        }
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        Module item = mItems.get(fromPosition);
        mItems.remove(fromPosition);
        mItems.add(toPosition, item);
        notifyItemMoved(fromPosition, toPosition);
    }


    public List<Module> getItem() {
        return mItems;
    }

    static class DragViewHolder extends RecyclerView.ViewHolder implements OnDragVHListener {
        TextView tv;
        ImageView img;
        View itemView;

        public DragViewHolder(View itemView) {
            super(itemView);
            this.itemView=itemView;
            tv = (TextView) itemView.findViewById(R.id.tv);
            img=(ImageView)itemView.findViewById(R.id.img_icon);
        }

        @Override
        public void onItemSelected() {
            itemView.setBackgroundColor(Color.LTGRAY);
        }

        @Override
        public void onItemFinish() {
            itemView.setBackgroundColor(0);
        }
    }
    private OnMyChannelItemClickListener mChannelItemClickListener;
    public interface OnMyChannelItemClickListener {
        void onItemClick(View v, int position);
    }

    public void setOnMyChannelItemClickListener(OnMyChannelItemClickListener listener) {
        this.mChannelItemClickListener = listener;
    }
    private OnMyChannelItemLongClickListener mChannelItemLongClickListener;
    public interface OnMyChannelItemLongClickListener {
        Boolean onItemLongClick(View v, int position);
    }

    public void setOnMyChannelItemLongClickListener(OnMyChannelItemLongClickListener listener) {
        this.mChannelItemLongClickListener = listener;
    }
}
